package com.im.file.component;

import java.util.concurrent.ConcurrentLinkedQueue;

import org.apache.log4j.Logger;

import com.im.file.task.UploadTask;

/**
 * @author XiaHui
 * @date 2015年3月10日 上午8:53:09
 */
public class UploadHandlerThread extends Thread {

	private static final Logger logger = Logger.getLogger(UploadHandlerThread.class);
	private ConcurrentLinkedQueue<UploadTask> queue;

	public UploadHandlerThread(ConcurrentLinkedQueue<UploadTask> queue) {
		this.queue = queue;
	}

	public void push(UploadTask task) {
		queue.add(task);
	}

	@Override
	public void run() {
		UploadTask task;
		while (true) {
			while (null != queue && queue.peek() != null) {
				task = queue.poll();
				task.execute();
				threadSleep(10);
			}
			threadSleep(500);
		}
	}

	private void threadSleep(long time) {
		try {
			sleep(time);
		} catch (InterruptedException e) {
			logger.error(e);
		}
	}
}
